using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;

namespace DragonAwakening.Game
{
    /// <summary>
    /// Character game.
    /// </summary>
    public class Character
    {
        /// <summary>
        /// Character image
        /// </summary>
        private Texture2D image;

        /// <summary>
        /// Health bar texture.
        /// </summary>
        private Texture2D healthBarTexture;

        /// <summary>
        /// Health bar texture.
        /// </summary>
        private Texture2D staminaBarTexture;

        /// <summary>
        /// Health bar texture.
        /// </summary>
        private Texture2D manaBarTexture;

        /// <summary>
        /// Font
        /// </summary>
        private SpriteFont font;

        /// <summary>
        /// Creates character.
        /// </summary>
        /// <param name="race"></param>
        public Character(string name, Race race)
        {
            this.Race = race;
            this.Name = name;

            this.Health = new MaxValueData() { Max = 100, Current = 50 };
            this.Stamina = new MaxValueData() { Max = 100, Current = 100 };
            this.Mana = new MaxValueData() { Max = 100, Current = 10 };
        }

        /// <summary>
        /// Gets the character race.
        /// </summary>
        public Race Race { get; private set; }

        /// <summary>
        /// Name
        /// </summary>
        public string Name { get; private set; }

        /// <summary>
        /// Health points.
        /// </summary>
        public MaxValueData Health { get; private set; }

        /// <summary>
        /// Stamina.
        /// </summary>
        public MaxValueData Stamina { get; private set; }

        /// <summary>
        /// Mana.
        /// </summary>
        public MaxValueData Mana { get; private set; }

        /// <summary>
        /// Loads assets.
        /// </summary>
        public void LoadContent(ContentManager content, GraphicsDevice device, SpriteFont font)
        {
            this.font = font;

            this.healthBarTexture = new Texture2D(device, 1, 1);
            this.healthBarTexture.SetData(new Color[] { Color.Red });

            this.staminaBarTexture = new Texture2D(device, 1, 1);
            this.staminaBarTexture.SetData(new Color[] { Color.Yellow });

            this.manaBarTexture = new Texture2D(device, 1, 1);
            this.manaBarTexture.SetData(new Color[] { Color.Blue });

            switch (this.Race)
            {
                case Race.Human:
                    this.image = content.Load<Texture2D>("CharacterHumanMale");
                    break;

                case Race.Elf:
                    this.image = content.Load<Texture2D>("CharacterElfFemale");
                    break;
            } 
        }

        /// <summary>
        /// Draws the character.
        /// </summary>
        public void Draw(SpriteBatch batch, Vector2 position)
        {
            batch.DrawString(font, this.Name, position, Color.White);

            position += new Vector2(0, 20);

            var facePosition = position + new Vector2(40, 0);
            batch.Draw(this.image, facePosition, Color.White);

            this.DrawBar(batch, position, this.Health, 1, this.healthBarTexture);
            this.DrawBar(batch, position, this.Stamina, 12, this.staminaBarTexture);
            this.DrawBar(batch, position, this.Mana, 23, this.manaBarTexture);
        }

        public void DrawBar(SpriteBatch batch, Vector2 position, MaxValueData data, int offset, Texture2D texture)
        {
            int barHeight = 90;
            var percent = (int)(data.Percentage * barHeight);
            var rect = new Rectangle((int)position.X + offset, (int)position.Y + barHeight - percent, 10, percent);
            batch.Draw(texture, rect, Color.White);
        }
    }
}
